package com.sofun.ai.config;

import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定义的记忆存储(实现与大模型聊天有记忆功能)
 * @author:wjl
 * @see:
 * @since:
 * @date:2025-03-04-19:42
 * @description:com.study.langchain4jspringboot.ai.memory
 * @version:1.0
 */
public class DbChatMemoryStore implements ChatMemoryStore {

    Map<String, List<ChatMessage>> memory = new HashMap<>();

    @Override
    public List<ChatMessage> getMessages(Object memoryId) {
        List<ChatMessage> chatMessages = memory.get(memoryId.toString());
        return chatMessages != null ? chatMessages : List.of();
    }

    @Override
    public void updateMessages(Object memoryId, List<ChatMessage> messages) {
        memory.put(memoryId.toString(), messages);
    }

    @Override
    public void deleteMessages(Object memoryId) {
        memory.remove(memoryId.toString());
    }
}
